/*
2017-04-25
*/
var ROOT='http://api.hongyanche.com';
CONSOLE = {
	urls:{
		label_rank		: ROOT+"/mall_admin/labels",
		label_modify	: ROOT+"/mall_admin/label_modify",

		config_template_create	: ROOT+"/mall_admin/config_template_create",
		config_template_remove	: ROOT+"/mall_admin/config_template_remove",
		config_template_release  : ROOT+"/mall_admin/config_template_release",
		config_template_range	: ROOT+"/mall_admin/config_template_range",
		config_template_modify	: ROOT+"/mall_admin/config_template_modify",

		config_list		: ROOT+"/mall_admin/config_getlist",
		config_create	: ROOT+"/mall_admin/config_create",
		config_remove	: ROOT+"/mall_admin/config_remove",
		config_release  : ROOT+"/mall_admin/config_release",
		config_range	: ROOT+"/mall_admin/config_range",
		config_rename	: ROOT+"/mall_admin/config_rename",
		config_descripton: ROOT+"/mall_admin/config_descripton",

		config_option_create	: ROOT+"/mall_admin/config_option_create",
		config_option_remove	: ROOT+"/mall_admin/config_option_remove",
		config_option_clean		: ROOT+"/mall_admin/config_option_clean",
		config_option_range		: ROOT+"/mall_admin/config_option_range",

		brand_list		: ROOT+"/mall_admin/product_getlist",
		brand_create	: ROOT+"/mall_admin/product_create",
		brand_remove	: ROOT+"/mall_admin/product_remove",
		brand_release   : ROOT+"/mall_admin/product_release",
		brand_range		: ROOT+"/mall_admin/product_range",
		brand_modify	: ROOT+"/mall_admin/product_modify",
		brand_highlight	: ROOT+"/mall_admin/product_highlight",

		brand_suggest   : ROOT+"/mall_admin/product_suggest",

		detail_list		: ROOT+"/mall_admin/detail_getlist",
		detail_create	: ROOT+"/mall_admin/detail_create",
		detail_remove	: ROOT+"/mall_admin/detail_remove",
		detail_release	: ROOT+"/mall_admin/detail_release",
		detail_range	: ROOT+"/mall_admin/detail_range",

		ueditor_img_upload :ROOT+"/upload/mall_article",

	},
	init:function(){
		this.editor.init();
		this.cashier.init();
	},
	editor:{
		init:function(){
			this.label.init();
			this.config.init();
			this.brand.init();
			this.detail.init();
			this.ueditor.init();
		},
		label:{
			init:function(){
				//启动滑块
				$(document).on('bc-rank-success','#editor-label-rank' , function(event) {
					$('#editor-label-rank-line').trigger('change')
					$(this).find('.bc-rank-content labels').each(function(index, el) {
						var r = $(this)	;
						Sortable.create(r.get(0),{
							group: "label",
							animation: 150,
							scrollSensitivity: 100,
							handle: '.i-move',
						})
						r.append('<label class="editor-label-edit editor-label-new"><div>+</div></label>')
					});
					Sortable.create($('#editor-label-rank-line').get(0),{
						group: "label-line",
						animation: 150,
						scrollSensitivity: 100,
						handle: '.i-menu',
					})
				});
				//编辑按钮
				$(document).on('click', '#editor-label-edit-btn',function(event) {
					var b = $('#editor-label-edit-btn');
					if(b.hasClass('active')){
						b.removeClass('active');
						b.text('编辑');
						b.trigger('editor-label-save')
						$('.editor-label-edit').hide();
						$('.editor-label-content').fadeIn();
					}else{
						b.addClass('active');
						b.text('完成');
						$('.editor-label-edit').fadeIn();
						$('.editor-label-content').hide();
					}
				});
				$(document).on('click', '.editor-label-new', function(event) {
					var self = $(this)
					var t = self.siblings().first().prop('outerHTML');
					self.before(t);
					self.prev().fadeIn();
				});
				$(document).on('click', '.editor-label-remove', function(event) {
					$(this).parents('label').fadeOut(150,function(){
						$(this).remove();
					})
				});
				
				$(document).on('click', '#editor-label-line-add', function(event) {
					var self = $('#editor-label-rank-line')
					var t = self.find('.bc-rank-template').first().prop('outerHTML');
					self.append(t);
					var line = self.children().last()
					line.fadeIn();
					line.addClass('bc-rank-content');
					line.addClass('active');
					line.removeClass('bc-rank-template');
					line.find('labels').children().hide();
					line.find('labels').append('<label class="editor-label-edit editor-label-new"><div>+</div></label>');
					line.find('.editor-label-edit').show().css('transform','');
					line.find('.editor-label-remove').fadeIn()
					Sortable.create(line.find('labels').get(0),{
						group: "label",
						animation: 150,
						scrollSensitivity: 100,
						handle: '.i-move',
					})
				});
				$(document).on('click', '.editor-label-line-remove', function(event) {
					$(this).parents('.bc-rank-content').fadeOut(150,function(){
						$(this).remove();
					})
				});
				POST.init(document,{
					method:'editor-label-save',
					input:function(){
						var v = [];
						// console.log($('#editor-label-rank-line .bc-rank-content'))
						$('#editor-label-rank-line .bc-rank-content').each(function(index, el) {
							v.push({category_cn:$(el).find('[name="category_text"]').val().trim(),labels:[]})
							$(el).find('labels').children().each(function(index2, el2) {
								var e = $(el2)
								var lb = e.find('[name="label_text"]');
								if(index2 && lb.length){
									// console.log(lb,lb.val(),{label_cn:lb.val()},v[index]);
									v[index].labels.push({label_cn:lb.val().trim()});
								}
							});
						});
						console.log(v);
						return {value:JSON.stringify(v)};
					},
					url:CONSOLE.urls.label_modify,
					text:'保存',
					confirm:true,
					cancel:function(){
						// RANK.reload('#editor-label-rank');
						// $('#editor-config-list').on('bc-list-success',function () {
						// 	RANK.reload('#editor-label-rank');
							$('#editor-config-list').addClass('hidden')

						// });


					},
					success:function(){
						$('#editor-label-rank').on('bc-rank-success',function () {
							RANK.reload('#editor-label-rank');
							RANK.reload('#editor-brand-label');
							RANK.reload('#editor-brand-detail-config');
						})
						$('#editor-config-list').hide()
					}
				},'#editor-label-edit-btn')

			},
			open:function(){
				var e = $('#editor-panel-label-config');
				e.fadeIn();
				e.siblings().hide();
			}
		},
		config:{
			labelSelected:function(){
				return $('#editor-config-labels').val();
			},
			listReload:function(){
				LIST.reload('#editor-config-list');
			},
			init:function(){
				var self = this;
				$(document).on('bc-page-success','.page.home', function(event) {
					Sortable.create($('#editor-config-list-content').get(0),{
						animation: 150,
						scrollSensitivity: 100,
						handle: '.i-menu',
						onUpdate:function(){
							$('#editor-config-list').trigger('editor-config-range')
						}
					})
				})

				$(document).on('bc-page-success','.page.home', function(event) {
					Sortable.create($('#editor-config-template-list-content').get(0),{
						animation: 150,
						scrollSensitivity: 100,
						handle: '.i-menu',
						onUpdate:function(){
							$('#editor-config-template-list').trigger('editor-config-template-range')
						}
					})
				})
				//refresh
				// $(document).on('change','#editor-label-rank-line', function(event) {
				// 	var c = $('[name$="label-edit"]:checked')
				// 	// var v = [];
				// 	var t = [];
				// 	c.each(function(index, el) {
				// 		// var label_id = $(el).parents('label').find('label_id').html();
				// 		// if(label_id) v.push(label_id);
				// 		var label_text = $(el).parents('label').find('label_text').html();
				// 		if(label_text) t.push(label_text);
				// 	});
				// 	// var v = v.join(',');
				// 	var t = t.join(',');
				// 	if(t == ''){t = '全部'}
				// 	// $("#editor-config-labels").val(v).trigger('change');
				// 	$("#editor-config-labels_text").html(t);
				// });
				//edit button
				$(document).on('click','#editor-config-edit-btn', function(event) {
					var b = $('#editor-config-edit-btn');
					if(b.hasClass('active')){
						self.listReload();
						$('#editor-config-list').on('bc-list-success',function () {
							b.removeClass('active');
							b.text('编辑');
							$('.editor-config-edit').hide();
							$('.editor-config-edit-range').fadeIn();
							$('.editor-config-content').fadeIn();

						});
					}else{
						$('#editor-config-list .bc-list-content config_options>label').addClass('editor-config-move');
						b.addClass('active');
						b.text('完成');
						$('.editor-config-edit').fadeIn();
						$('.editor-config-edit-range').hide();
						$('.editor-config-content').hide();
					}
				});

				$(document).on('click','#editor-config-template-edit-btn', function(event) {
					var b = $('#editor-config-template-edit-btn');
					if(b.hasClass('active')){
						b.removeClass('active');
						b.text('编辑');
						$('.editor-config-template-edit').hide();
						$('.editor-config-template').fadeIn();
						$('.editor-config-template-edit-range').fadeIn();
						LIST.reload('#editor-config-template-list');
						$('#editor-config-template-list').on('bc-list-success',function () {
							$('.editor-config-template-content').fadeIn();

						});
					}else{
						$('#editor-config-template-list .bc-list-content').addClass('editor-config-template-move');
						b.addClass('active');
						b.text('完成');
						$('.editor-config-template-edit').fadeIn();
						$('.editor-config-template').hide();
						$('.editor-config-template-content').hide();
						$('.editor-config-template-edit-range').hide();
					}
				});



				//range
				POST.init(document,{
					method:"editor-config-range",
					input:function(){
						var range = [];
						$('#editor-config-list .bc-list-content').children().each(function(index, el) {
							range.push($(el).attr('data-id'))
						});
						range = range.join(',');
						return {range:range}
					},
					url:CONSOLE.urls.config_range,
					style:'none',
				},'#editor-config-list')
				//add
				POST.init(document,{
					input:function(e){
						// var labels = self.labelSelected();
						var tamplate = $('#template_id').val()
						return {config_template_id:tamplate}
					},
					text:'添加',
					style:'none',
					success:function(){
						self.listReload();
					},
					url:CONSOLE.urls.config_create
				}, '#editor-config-create')
				//release
				POST.init(document,{
					method:'change',
					stop:function(e){
						var config_id = e.parents('.bc-list-line').find('config_id').html();
						if(!config_id){
							return true;
						}
					},
					input:function(e){
						var e = $(e);
						var release = parseInt(e.prop('checked')*1) || 0;
						var config_id = e.parents('.bc-list-line').find('config_id').html();
						return {config_id:config_id,release:release}
					},
					style:'input',
					url:CONSOLE.urls.config_release
				},'.editor-config-release-switch')
				//newname
				POST.init(document,{
					method:'change',
					stop:function(e){
						var config_id = e.parents('.bc-list-line').find('config_id').html();
						if(!config_id){
							return true;
						}
					},
					text:'重命名',
					input:function(e){
						var e = $(e);
						var newname = e.val();
						var config_id = e.parents('.bc-list-line').find('config_id').html();
						return {config_id:config_id,newname:newname}
					},
					style:'input',
					url:CONSOLE.urls.config_rename,
					success:function(e){
						$(e).parents('.bc-list-line').find('config_name_cn').html($(e).val())
					}
				},'.editor-config-name-input')
				//描述
				POST.init(document,{
					method:'change',
					stop:function(e){
						var config_id = e.parents('.bc-list-line').find('config_id').html();
						if(!config_id){
							return true;
						}
					},
					text:'重命名',
					input:function(e){
						var e = $(e);
						var config_descripton = e.val();
						var config_id = e.parents('.bc-list-line').find('config_id').html();
						return {config_id:config_id,config_descripton:config_descripton}
					},
					style:'input',
					url:CONSOLE.urls.config_descripton,
					success:function(e){
						$(e).parents('.bc-list-line').find('confing_descripton').html($(e).val())
					}
				},'.editor-config-descripton-input')
				//remove
				POST.init(document,{
					stop:function(){
						var config_id = LIST.selected('#editor-config-list');
						if(!config_id.length){
							return true;
						}
					},
					confirm:function(){
						return {target:LIST.selected('#editor-config-list').length+"个配置"}
					},
					text:'删除',
					input:function(){
						var config_id = LIST.selected('#editor-config-list');
						config_id = config_id.join(',')
						return {config_id:config_id}
					},
					url:CONSOLE.urls.config_remove,
					success:function(){
						self.listReload();
					}
				},'#editor-config-remove')
				//option_remove
				POST.init(document,{
					stop:function(e){
						var config_option_id = e.parents('label').find('config_option_id').html();
						if(!config_option_id || config_option_id==''){
							return true;
						}
					},
					text:'删除',
					input:function(e){
						var e = $(e);
						var config_option_id = e.parents('label').find('config_option_id').html();
						return {config_option_id:config_option_id}
					},
					style:'none',
					success:function(e){
						var e = $(e).parents('label');
						e.fadeOut(120,function(){
							e.remove();
						})
					},
					url:CONSOLE.urls.config_option_remove,
				},'.editor-config-option-remove')
				POST.init(document,{
					stop:function(e){
						var config_id = e.parents('.bc-list-line').attr('data-id');
						if(!config_id || config_id==''){
							return true;
						}
					},
					text:'清空',
					confirm:true,
					input:function(e){
						var e = $(e);
						var config_id = e.parents('.bc-list-line').attr('data-id');
						return {config_id:config_id}
					},
					success:function(e){
						var e = $(e).siblings('config_options').children().first().siblings();
						e.fadeOut(120,function(){
							e.remove();
						})
					},
					url:CONSOLE.urls.config_option_clean,
				},'.editor-config-option-clean')
				//option_add
				POST.init(document,{
					method:'change',
					stop:function(e){
						var config_id = e.parents('.bc-list-line').find('config_id').html();
						if(!config_id){
							return true;
						}
					},
					text:'添加',
					style:'none',
					input:function(e){
						var e = $(e);
						var config_option_name = e.val();
						e.val('');
						var config_id = e.parents('.bc-list-line').find('config_id').html();
						return {config_id:config_id,config_option_name:config_option_name}
					},
					// style:'input',
					success:function(e,res){
						var e = $(e).parents('.bc-list-line').find('config_options').eq(0);
						var v = res.data.config_options;
						// var v = [{},{},{},{}]
						console.log(e,v)
						VALUE.write(e,'list',v);
					},
					url:CONSOLE.urls.config_option_create,
				},'.editor-config-option-create')
				//option_range
				$(document).on('bc-list-success', '#editor-config-list', function(event) {
					$(this).find('.bc-list-content').children().each(function(index, el) {
						Sortable.create($(el).find('config_options').get(0),{
							animation: 150,
							scrollSensitivity: 100,
							handle: '.editor-config-move',
							onUpdate:function(evt){
								$(evt.item).parents('config_options').trigger('editor-config-option-range');
							}
						})
					});
				});
				POST.init(document,{
					method:'editor-config-option-range',
					style:'none',
					stop:function(e){
						var config_id = $(e).parents('.bc-list-line').attr('data-id');
						if(!config_id) return true;
					},
					input:function(e){
						var line = $(e).parents('.bc-list-line')
						var config_id = line.attr('data-id');
						var range = [];
						line.find('config_options').children().each(function(index, el) {
							if(index) range.push($(el).find('config_option_id').html())
						});
						range = range.join(',');
						return {config_id:config_id,range:range};
					},
					url:CONSOLE.urls.config_option_range,
				},'#editor-config-list config_options');


				//template range
				POST.init(document,{
					method:"editor-config-template-range",
					input:function(){
						var range = [];
						$('#editor-config-template-list .bc-list-content').children().each(function(index, el) {
							range.push($(el).attr('data-id'))
						});
						range = range.join(',');
						return {range:range}
					},
					url:CONSOLE.urls.config_template_range,
					style:'none',
				},'#editor-config-template-list')

				//template  release
				POST.init(document,{
					method:'change',
					stop:function(e){
						var config_template_id = e.parents('.bc-list-line').find('config_template_id').html();
						if(!config_template_id){
							return true;
						}
					},
					input:function(e){
						var e = $(e);
						var release = parseInt(e.prop('checked')*1) || 0;
						var config_template_id = e.parents('.bc-list-line').find('config_template_id').html();
						return {config_template_id:config_template_id,release:release}
					},
					style:'input',
					url:CONSOLE.urls.config_template_release
				},'.editor-config-template-release-switch')
				//template  newname
				POST.init(document,{
					method:'change',
					stop:function(e){
						var config_template_id = e.parents('.bc-list-line').find('config_template_id').html();
						if(!config_template_id){
							return true;
						}
					},
					text:'重命名',
					input:function(e){
						var e = $(e);
						var newname = e.val();
						var config_template_id = e.parents('.bc-list-line').find('config_template_id').html();
						return {config_template_id:config_template_id,new_template_name:newname}
					},
					style:'input',
					url:CONSOLE.urls.config_template_modify,
					success:function(e){
						$(e).parents('.bc-list-line').find('template_name').html($(e).val())
					}
				},'.editor-config-template-name-input')
				//remove
				POST.init(document,{
					stop:function(){
						var config_template_id = LIST.selected('#editor-config-template-list');
						if(!config_template_id.length){
							return true;
						}
					},
					confirm:function(){
						return {target:LIST.selected('#editor-config-template-list').length+"个配置"}
					},
					text:'删除',
					input:function(){
						var config_template_id = LIST.selected('#editor-config-template-list');
						config_template_id = config_template_id.join(',')
						return {config_template_id:config_template_id}
					},
					url:CONSOLE.urls.config_template_remove,
					success:function(){
						LIST.reload('#editor-config-template-list')

						var b = $('#editor-config-template-edit-btn');
						if(b.hasClass('active')){
							b.removeClass('active');
							b.text('编辑');
							$('.editor-config-template-edit').hide();
							$('.editor-config-template').fadeIn();
							$('.editor-config-template-edit-range').fadeIn();
							LIST.reload('#editor-config-template-list');
							$('#editor-config-template-list').on('bc-list-success',function () {
								$('.editor-config-template-content').fadeIn();
							});
						}else{
							$('#editor-config-template-list .bc-list-content').addClass('editor-config-template-move');
							b.addClass('active');
							b.text('完成');
							$('.editor-config-template-edit').fadeIn();
							$('.editor-config-template').hide();
							$('.editor-config-template-content').hide();
							$('.editor-config-template-edit-range').hide();
						}
					}
				},'#editor-config-template-remove')

			}
		},
		brand:{
			init:function(){
				var self = this;
				//
				POST.init(document,{
					input:function(){
						var labels = $('#editor-brand-list-labels-input').val();
						return {labels:labels};
					},
					text:'添加',
					success:function(){
						LIST.reload('#editor-brand-list')
					},
					url:CONSOLE.urls.brand_create
				},"#editor-brand-create")
				//remove
				POST.init(document,{
					stop:function(){
						return LIST.selected('#editor-brand-list').length <=0;
					},
					input:function(){
						var mall_id = LIST.selected('#editor-brand-list');
						mall_id = mall_id.join(',');
						return {mall_id:mall_id};
					},
					text:'删除',
					confirm:function(){
						return {target:LIST.selected('#editor-brand-list').length+"个产品"}
					},
					success:function(){
						CONSOLE.editor.label.open()
						LIST.reload('#editor-brand-list')
					},
					url:CONSOLE.urls.brand_remove
				},"#editor-brand-remove")
				//modify
				POST.init(document,{
					method:'change',
					url:CONSOLE.urls.brand_modify,
					style:'input',
					stop:function(){
						var mall_id = $("#option-brand-detail-mall_id").html();
						if(!mall_id) return true;
					},
					input:function(e){
						var mall_id = $("#option-brand-detail-mall_id").html();
						var key = $(e).attr('name');
						var content = $(e).val();
						return {mall_id:mall_id,key:key,content:content}
					},
					success:function(){
						LIST.reload('#editor-brand-list')
					}
				},'.editor-brand-modify')
				//release
				POST.init(document,{
					method:'change',
					url:CONSOLE.urls.brand_release,
					style:'input',
					stop:function(){
						var mall_id = $("#option-brand-detail-mall_id").html();
						if(!mall_id) return true;
					},
					input:function(e){
						var mall_id = $("#option-brand-detail-mall_id").html();
						var release = parseInt($(e).prop('checked')*1) || 0;
						return {mall_id:mall_id,release:release}
					},
					success:function(){
						LIST.reload('#editor-brand-list')
					}
				},'#editor-brand-released')
				//suggest
				POST.init(document,{
					method:'change',
					url:CONSOLE.urls.brand_suggest,
					style:'input',
					stop:function(){
						var mall_id = $("#option-brand-detail-mall_id").html();
						if(!mall_id) return true;
					},
					input:function(e){
						var mall_id = $("#option-brand-detail-mall_id").html();
						var suggest = parseInt($(e).prop('checked')*1) || 0;
						return {mall_id:mall_id,suggest:suggest}
					},
					success:function(){
						LIST.reload('#editor-brand-list')
					}
				},'#editor-brand-suggest')
			},
			open:function(e){
				$('#editor-panel-label-config').hide();
				$('#editor-panel-brand-detail').fadeIn();
				DETAIL.put($('#editor-panel-brand-detail'),$(e).find('.bc-list-detail-box'));
				// $(window).scrollTop(0)
			}
		},
		detail:{
			init:function(){
				//create
				POST.init(document,{
					stop:function(){
						var mall_id = $("#option-brand-detail-mall_id").html();
						if(!mall_id) return true;
						var price = parseInt($('#editor-detail-price-input').val()*100);
						if(!price){
							alert('请填写正确的价格')
							$('#editor-detail-price-input').select();
							$('#editor-detail-price-input').focus();
							return true;
						}
					},
					input:function(e){
						var mall_id = $("#option-brand-detail-mall_id").html();
						var price = parseInt($('#editor-detail-price-input').val()*100);
						var discount_price = parseInt($('#editor-detail-discount_price-input').val()*100);
						var deposit = parseInt($('#editor-detail-deposit-input').val()*100);
						var code = $('#editor-detail-code-input').val();
						var options = {};
						$('#editor-detail-config-list .bc-list-content').children().each(function(index, el) {
							var c = $(el).attr('data-id');
							var v = $(el).find('.editor-config-option').val();
							if(c && v){
								if(!options[c]) options[c] = {};
								options[c]['option_name_cn'] = v;
							}
						});
						options = JSON.stringify(options);
						var ret = {mall_id:mall_id,price:price,code:code,options:options}
						var discount = parseInt($('#editor-detail-discount-input').val());
						if(discount) ret['discount'] = discount;
						if(discount_price) ret['discount_price'] = discount_price;
						if(deposit) ret['deposit'] = deposit;
						var discount_none = $('#editor-detail-create-discount_none').prop('checked');
						if(discount_none) ret['discount_none'] = 1;
						var admin_only = $('#editor-detail-create-admin_only').prop('checked');
						if(admin_only) ret['admin_only'] = 1;
						return ret;
					},
					text:'添加',
					url:CONSOLE.urls.detail_create,
					success:function(){
						LIST.reload('#editor-detail-list');
					}
				},'#editor-detail-create')
				$(document).on('change','#editor-detail-config-list .weui-select',function(){
					var e = $(this);
					var v = e.val();
					console.log(e,v)
					e.parents('.bc-list-line').find('.editor-config-option').val(v);
					e.val('')
				})
				$(document).on('bc-list-success', '#editor-detail-config-list', function(event) {
					$(this).find('.bc-list-content').children().each(function(index, el) {
						var options = $(el).find('config_options').html();
						options = eval("("+options+")");
						for(var i in options){
							var option = options[i]['option_name_cn'];
							$(el).find('.weui-select').append('<option>'+option+'</option>')
						}
					});
				});
				//remove
				POST.init(document,{
					stop:function(){
						var detail_id = LIST.selected('#editor-detail-list');
						if(!detail_id.length){
							return true;
						}
					},
					confirm:function(){
						return {target:LIST.selected('#editor-detail-list').length+"个价格"}
					},
					text:'删除',
					input:function(){
						var detail_id = LIST.selected('#editor-detail-list');
						detail_id = detail_id.join(',')
						return {detail_id:detail_id}
					},
					url:CONSOLE.urls.detail_remove,
					success:function(){
						LIST.reload('#editor-detail-list');
					}
				},'#editor-detail-remove')
				//release
				POST.init(document,{
					input:function(e){
						var detail_id = $(e).parents('.bc-list-line').attr('data-id');
						var release = parseInt($(e).prop('checked')*1) || 0;
						return {detail_id:detail_id,release:release}
					},
					stop:function(e){
						var detail_id = $(e).parents('.bc-list-line').attr('data-id');
						if(!detail_id) return true;
					},
					style:'input',
					url:CONSOLE.urls.detail_release,
				},".editor-detail-release")
				//range
				$(document).on('bc-list-success', '#editor-detail-list', function(event) {
					Sortable.create($('#editor-detail-list .bc-list-content').get(0),{
						animation: 150,
						scrollSensitivity: 100,
						handle:".i-menu",
						onUpdate:function(){
							$('#editor-detail-list').trigger('editor-detail-range')
						}
					})
				});
				POST.init(document,{
					method:"editor-detail-range",
					input:function(){
						var range = [];
						$('#editor-detail-list .bc-list-content').children().each(function(index, el) {
							range.push($(el).attr('data-id'))
						});
						range = range.join(',');
						return {range:range}
					},
					url:CONSOLE.urls.detail_range,
					style:'none',
				},'#editor-detail-list')
			}
		},
		ueditor:{
			init:function(){
				var ue = null;
				$(document).on('bc-page-success', '.page.article', function(event) {
					var mall_id = $("#option-brand-detail-mall_id").html();
					var name = $(".editor-brand-modify[name='name']").val();
					if(!mall_id){
						if(PAGE.stack.length){history.back();}else{location.href='#editor'}
					}
					$("#editor-ueditor name").html(name)
					$("#editor-ueditor mall_id").html(mall_id)
					if(ue) ue.destroy();
					ue = UE.getEditor('myEditor',{
						imageUrl:CONSOLE.urls.ueditor_img_upload+"?mall_id="+mall_id,
						imagePath:"",
                        toolbars: [
                            [
                                'undo', //撤销
                                'redo', //重做
                                'bold', //加粗
                                'indent', //首行缩进
                                'italic', //斜体
                                'underline', //下划线
                                'strikethrough', //删除线
                                'subscript', //下标
                                'fontborder', //字符边框
                                'superscript', //上标
                                'formatmatch', //格式刷
                                'blockquote', //引用
                                'horizontal', //分隔线
                                'removeformat', //清除格式
                                'insertrow', //前插入行
                                'insertcol', //前插入列
                                'mergeright', //右合并单元格
                                'mergedown', //下合并单元格
                                'deleterow', //删除行
                                'deletecol', //删除列
                                'splittorows', //拆分成行
                                'splittocols', //拆分成列
                                'splittocells', //完全拆分单元格
                                'deletecaption', //删除表格标题
                                'inserttitle', //插入标题
                                'mergecells', //合并多个单元格
                                'deletetable', //删除表格
                                'cleardoc', //清空文档
                                'insertparagraphbeforetable', //"表格前插入行"
                                'fontfamily', //字体
                                'fontsize', //字号
                                'paragraph', //段落格式
                                'simpleupload', //单图上传
                                'insertimage', //多图上传
                                'edittable', //表格属性
                                'edittd', //单元格属性
                                'link', //超链接
                                'emotion', //表情
                                'spechars', //特殊字符
                                'searchreplace', //查询替换
                                'insertvideo', //视频
                                'justifyleft', //居左对齐
                                'justifyright', //居右对齐
                                'justifycenter', //居中对齐
                                'justifyjustify', //两端对齐
                                'forecolor', //字体颜色
                                'backcolor', //背景色
                                'insertorderedlist', //有序列表
                                'insertunorderedlist', //无序列表
                                'fullscreen', //全屏
                                'rowspacingtop', //段前距
                                'rowspacingbottom', //段后距
                                'pagebreak', //分页
                                'imagenone', //默认
                                'imageleft', //左浮动
                                'imageright', //右浮动
                                'attachment', //附件
                                'imagecenter', //居中
                                'wordimage', //图片转存
                                'lineheight', //行间距
                                'edittip ', //编辑提示
                                'customstyle', //自定义标题
                                'touppercase', //字母大写
                                'tolowercase', //字母小写
                                'scrawl', //涂鸦
                                'inserttable', //插入表格
                            ]
                        ]
					});
					var article = $("#brand-editor-article").html();
					if(article){
						ue.addListener("ready", function () {
							// editor准备好之后才可以使用
							ue.setContent(article);

						});
					}else{
						// um.setContent(text,false);
					}
				});
				POST.init(document,{
					text:"保存文案",
					confirm:true,
					url:CONSOLE.urls.brand_modify,
					stop:function(){
						var mall_id = $("#option-brand-detail-mall_id").html();
					},
					input:function(e){
						var mall_id = $("#option-brand-detail-mall_id").html();
						var content = ue.getContent();
						return {mall_id:mall_id,key:'article',content:content}
					},
					success:function(){
						LIST.reload('#editor-brand-list');
						var content = ue.getContent();
						$("#brand-editor-article").html(content);
					}
				},'#brand-article-modify')
			}
		}
	},
	cashier:{
		init:function(){
			$(document).on('bc-modal-success','#cashier-refund', function(event) {
				var total_fee = parseFloat($(this).find('total_fee').html().replace(/,/g,''));
				var refund_fee = parseFloat($(this).find('refund_fee').html().replace(/,/g,''));
				var refund_available = total_fee - refund_fee;
				refund_available = (refund_available.toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
				$("#cashier-refund-refund_fee").val('')
				$("#cashier-refund-refund_fee").attr('placeholder',"可退款金额："+refund_available)

				$('#cashier-refund .cashier-order-detail-line').each(function(index, el) {
					if(index){
						var e = $(el);
						var remain = parseInt(e.find('remain').html())
						var amount = parseInt(e.find('amount').html())
						if(remain === 0){
							e.find('remain').removeAttr('style');
						}
						e.find('remain').html(amount-remain);
						var options = e.find('options').html();
						if(options && options.length){
							options = eval("("+options+")");
							var option_names = [];
							for(var i in options){
								option_names.push(options[i]['option_name_cn']);
							}
							option_names = option_names.join(",");
							e.find('options').html(option_names).removeClass('hidden');
						}
					}
				});
			});
			//refund
			var refund_fee,refund_order_id;
			POST.init(document,{
				url:"/pay/refund",
				success:function(){
					MODAL.close('.modal')
					LIST.reload('#cashier-order-list');
					LIST.reload('#cashier-refund-list');
					INFO.reload('#cashier-briefing');
				},
				text:"退款",
				input:function(){
					return {order_id:refund_order_id,refund_fee:refund_fee}
				},
				confirm:true,
				stop:function(){
					refund_order_id = 0;refund_fee=0;
					refund_fee = parseInt(parseFloat($('#cashier-refund-refund_fee').val())*100)
					if(!refund_fee) return true;
					refund_order_id = $('#cashier-refund-order_id').html();
					if(!refund_order_id) return true;
				}
			},'#cashier-refund-confirm')
		},
		selectDetail:function(t){
			$('#mall-menu-list').hide();
			$('#mall-menu-detail').fadeIn();
		}
	}
}
CONSOLE.init();